package com.css.dzj.zfjg.modules.riskwarning.service.impl;


import com.css.dzj.zfjg.modules.riskwarning.entity.JgLhjgRiskWarningEntity;
import com.css.dzj.zfjg.modules.riskwarning.entity.QJgLhjgRiskWarningEntity;
import com.css.dzj.zfjg.modules.riskwarning.mapper.LhjgRiskWarningMapper;
import com.css.dzj.zfjg.modules.riskwarning.service.JgLhjgRiskWarningService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service()
public class JgLhjgRiskWarningServiceImpl  implements JgLhjgRiskWarningService {
    @Autowired
    private LhjgRiskWarningMapper lhjgRiskWarningMapper;

    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Override
    public QueryResults<JgLhjgRiskWarningEntity> queryPage(Pageable pageable) {

        QJgLhjgRiskWarningEntity q = QJgLhjgRiskWarningEntity.jgLhjgRiskWarningEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        return  jpaQueryFactory
                .selectFrom(q)
                .where(
                        predicate
                )
                .orderBy(q.createAt.desc())
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }

    @Override
    public JgLhjgRiskWarningEntity findByTaskNo(String taskNo) {
        QJgLhjgRiskWarningEntity q = QJgLhjgRiskWarningEntity.jgLhjgRiskWarningEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskNo.eq(taskNo)).fetchOne();
    }

    @Override
    public JgLhjgRiskWarningEntity getById(String riskId) {
        QJgLhjgRiskWarningEntity q = QJgLhjgRiskWarningEntity.jgLhjgRiskWarningEntity;
        return  jpaQueryFactory
                .selectFrom(q)
                .where(q.riskId.eq(riskId)
                )
                .fetchOne();
    }

    @Override
    public String save(JgLhjgRiskWarningEntity jgLhjgRiskWarning) {
        lhjgRiskWarningMapper.save(jgLhjgRiskWarning);
        return null;
    }

    @Override
    public String removeByIds(List list) {
        for (int i = 0;i<list.size();i++){
            String id = (String) list.get(i);
            lhjgRiskWarningMapper.deleteById(id);
        }
        return null;
    }

}